{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Output()"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import k3d\n",
    "import math\n",
    "import numpy as np\n",
    "\n",
    "bodies_count = 25\n",
    "bodies = np.random.random_sample((bodies_count, 7))\n",
    "bodies[:, 0:6] -= 0.5\n",
    "bodies[:, 3:6] *= 0.05\n",
    "bodies[:,6] = (bodies[:,6] + 0.5) * 1000\n",
    "bodies[0,:] = np.array([0,0,0,0,0,0,1e6])\n",
    "\n",
    "for i in range(1, bodies_count):\n",
    "    bodies[i, 0:3] = (bodies[i, 0:3] / np.linalg.norm(bodies[i, 0:3])) * 0.5\n",
    "\n",
    "plot = k3d.plot(antialias=True)\n",
    "points = k3d.points(bodies[:, 0:3].astype(np.float32), color=0, point_size=0.05)\n",
    "plot += points\n",
    "plot.display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot.camera_auto_fit = False\n",
    "plot.grid_auto_fit = False\n",
    "\n",
    "G = 6.67E-11\n",
    "lines = []\n",
    "speeds = []\n",
    "positions_animated = {}\n",
    "\n",
    "for i in range(bodies_count):\n",
    "    lines.append([])\n",
    "    speeds.append([])\n",
    "    \n",
    "for t in range(500):    \n",
    "    for i in range(bodies_count):\n",
    "        sum_force = np.zeros(3)\n",
    "        \n",
    "        for j in range(bodies_count):\n",
    "            if i == j:\n",
    "                continue\n",
    "            \n",
    "            direction = bodies[j, 0:3] - bodies[i, 0:3]            \n",
    "            force = G * bodies[i, 6] * bodies[j, 6] * direction\n",
    "            force = force / (np.linalg.norm(direction) ** 3)\n",
    "            sum_force = sum_force + force\n",
    "\n",
    "        bodies[i, 3:6] = bodies[i, 3:6] + sum_force / bodies[i, 6]\n",
    "    \n",
    "    for i in range(bodies_count):\n",
    "        bodies[i, 0:3] = bodies[i, 0:3] + bodies[i, 3:6] * 0.2\n",
    "        lines[i].append(np.copy(bodies[i, 0:3]))\n",
    "        speeds[i].append(np.linalg.norm(bodies[i, 3:6]))\n",
    "    \n",
    "    points.positions = np.copy(bodies[:, 0:3]).astype(np.float32)\n",
    "    positions_animated[str(t / 25.0)] = points.positions.copy()\n",
    "\n",
    "for line, speed in zip(lines, speeds):\n",
    "    plot += k3d.line(line, attribute=speed, shader=\"mesh\", width=0.0025, \n",
    "                     color_range=[0,0.1], color_map=k3d.basic_color_maps.Jet)    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Time series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "points.positions = positions_animated"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = plot.get_binary_snapshot()\n",
    "\n",
    "with open('binary_snapshot.k3d', 'wb') as f:\n",
    "    f.write(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot.time = 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![Hello World]()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "or"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot.start_auto_play()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot.stop_auto_play()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
